/*****************************************************
** Author:
** Date: 2019-10-19
*
*****************************************************/

#ifndef __COMM_H_
#define __COMM_H_

#ifdef __cplusplus
extern "C"{
#endif

#ifdef OS_LINUX
#include <stdio.h>
#include <string.h>
#include "uart_if.h"
#include "syscom.h"
#elif defined(OS_DNA)
#include "dna_libc.h"
#include "dna_errno.h"
#include "dna_profile.h"
#include "dna_uart.h"
#include "dna_os.h"
#endif

#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"


#define LUA_STACK_DEBUG
#define LUA_LOG_DEBUG

#ifdef LUA_LOG_DEBUG
#define lua_log_dbg(_fmt_, ...)     \
    printf("[lua-linux][DBG][%s:%d] "_fmt_"\n", __func__, __LINE__, ##__VA_ARGS__)
#else
#define lua_log_dbg(_fmt_, ...)
#endif

#define lua_log_inf(_fmt_, ...)     \
    printf("[lua-linux][INF] "_fmt_"\n", ##__VA_ARGS__)
#define lua_log_wrn(_fmt_, ...)     \
    printf("[lua-linux][WRN] "_fmt_"\n", ##__VA_ARGS__)
#define lua_log_err(_fmt_, ...)     \
    printf("[lua-linux][ERR][%s:%d] "_fmt_"\n", __func__, __LINE__, ##__VA_ARGS__)


#define DeepOfArray(a)              (sizeof(a) / sizeof(*(a)))
#define CountOfString(a)            (sizeof(a) - 1)

#define Swap(a, b)                  do { a ^= b; b ^= a; a ^= b; } while (0)
#define TenthFloor(a, b)			(a - a % b)

#define LUA_ARCH_MAGIC		0xdeadc0de

/*
 *	LUA_ARCH Error Code
 */
enum {
	LUA_ARCH_OK = 0,
	LUA_ARCH_FAILED,
	LUA_ARCH_ERR_SYS,
	LUA_ARCH_ERR_SYSCOM,
	LUA_ARCH_ERR_SYSTMR,
	LUA_ARCH_ERR_NO_MEM,
	LUA_ARCH_ERR_SCRIPT,
	LUA_ARCH_ERR_VM,
	LUA_ARCH_ERR_PARAM,
};

struct lua_arch_uart
{
	int magic;
	int id;
    int baud;
    int bits;
    int parity;
    int stop;
    int fit;
    unsigned char *buff;
    unsigned int maxsize;
    unsigned int buffsize;
    unsigned int working;
    unsigned long tick;

#ifdef OS_LINUX
    int hdl;
#elif defined(OS_DNA)
    dna_uart_handle_t hdl;
    dna_mutex_handle_t mutex;
#endif
};

struct lua_arch_devinfo
{
	char name[62];
    unsigned char did[6];
    unsigned char pid[16];
    unsigned short devtype;
};

void print_lua_stack(lua_State* lua, char *str);
void lua_arch_pushtable(lua_State *l, unsigned char *input, int inlen);
void dump_memory(char *str, const unsigned char *buff, unsigned int len);
int str2hex(char *str, unsigned char *strhex, int maxsize);


#ifdef __cplusplus
}
#endif

#endif /* __COMM_H_ */

